/******************************************************************************
 * Copyright (C) 2016 Wuhan Water Elephant Co.Ltd All Rights Reserved. 
 * 本软件为武汉水象科技有限公司开发研制。 未经本公司正式书面同意，其他任何个人、
 * 团体不得使用、复制、修改或发布本软件.
 *****************************************************************************/
package com.sxfq.loansupermarket.app.index.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.sxfq.loansupermarket.app.index.service.IndexService;
import com.sxfq.loansupermarket.business.cms.entity.CmsChannel;
import com.sxfq.loansupermarket.business.cms.entity.CmsContent;
import com.sxfq.loansupermarket.business.cms.service.CmsChannelService;
import com.sxfq.loansupermarket.business.cms.service.CmsContentService;
import com.sxfq.loansupermarket.common.utils.StringUtil;

import tk.mybatis.mapper.entity.Example;

/**
 * 
 * 
 * Module:
 * 
 * IndexServiceImpl.java
 * 
 * @author 程盼
 * @since JDK 1.8
 * @version 1.0
 * @description: <描述>
 */
@Service
public class IndexServiceImpl implements IndexService {

	@Resource
	private CmsChannelService cmsChannelService;

	@Resource
	private CmsContentService cmsContentService;

	private List<CmsContent> getContentList(String channelId) {
		Example example = new Example(CmsContent.class);
		example.createCriteria().andEqualTo("channelId", channelId);// 栏目ID
		example.createCriteria().andEqualTo("status", 3);// 审核状态(3通过)
		example.setOrderByClause(" create_time desc ");
		List<CmsContent> contentList = cmsContentService.selectByExample(example);
		return contentList;
	}

	/**
	 * 首页bannar
	 * 
	 * @see com.sxfq.loansupermarket.app.index.service.IndexService#getContentList(java.lang.String)
	 */
	@Override
	public List<Map<String, Object>> getBannar(String channelId) {
		List<CmsContent> contentList = getContentList(channelId);
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		if (!CollectionUtils.isEmpty(contentList)) {
			for (CmsContent cms : contentList) {
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("titleImg", StringUtil.toString(cms.getTitleImg()));// 标题图片
				map.put("url", StringUtil.toString(cms.getUrl()));// 外部链接
				list.add(map);
			}
		}
		return list;
	}

	/**
	 * 帮助中心
	 * 
	 * @see com.sxfq.loansupermarket.app.index.service.IndexService#getHelpCenter(java.lang.String)
	 */
	@Override
	public List<Map<String, Object>> getHelpCenter(String channelId) {
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		Example example = new Example(CmsChannel.class);
		example.createCriteria().andEqualTo("parentId", channelId);// 父栏目ID
		example.setOrderByClause(" priority asc ");// 排序
		List<CmsChannel> channelList = cmsChannelService.selectByExample(example);
		if (!CollectionUtils.isEmpty(channelList)) {
			for (CmsChannel channel : channelList) {
				Map<String, Object> channelMap = new HashMap<String, Object>();
				List<CmsContent> contentList = getContentList(StringUtil.toString(channel.getId()));
				List<Map<String, Object>> tmpList = new ArrayList<Map<String, Object>>();
				if (!CollectionUtils.isEmpty(contentList)) {
					for (CmsContent cms : contentList) {
						Map<String, Object> contentMap = new HashMap<String, Object>();
						contentMap.put("title", cms.getTitle());// 标题
						contentMap.put("detail", cms.getDetail());// 详情
						tmpList.add(contentMap);
					}
					channelMap.put("channel", channel.getChannelName());
					channelMap.put("contentList", tmpList);
					list.add(channelMap);
				}
			}
		}
		return list;
	}

}
